import axios from "axios"
import router from "../config/router";
import {
    Message,
    MessageBox
} from 'element-ui'
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  let token = localStorage.getItem("token")
    config.headers.common['token'] = token;
    config.headers.common['id'] = localStorage.getItem("id");
  //config.headers.post["Content-type"] = 'application/x-www-form-urlencoded;charset=UTF-8'
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});


// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
    if (response.data.result == 2) {
        Message({
            message: response.data.message,
            type: 'error',
            duration: 5 * 1000
        })
    sessionStorage.clear()
    router.replace({
      path:'/login',
      query:{
        redirect:router.currentRoute.fullPath
      }
    })
    return;
  }
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});

let baseUrl = '/api';
// let baseUrl = 'http://180.76.236.187:8088';


//get请求
export const GET = (url, params) => {
  return axios.get(`${baseUrl}${url}`, params, {
  }).then(data => data)
}


//post请求
export const POST = (url, data) => {
  return axios.post(`${baseUrl}${url}`, data, {
  }).then(data => data)
}

//delete请求
export const DELETE = (url, params) => {
  return axios.delete(`${baseUrl}${url}`, params, {
  }).then(data => data)
}


//put请求
export const PUT = (url, data) => {
  return axios.put(`${baseUrl}${url}`, data, {
  }).then(data => data)
}